//
//  SYTradeInputView.m
//  SYKeyboard
//
//  Created by hc on 2019/3/22.
//  Copyright © 2019 hc. All rights reserved.
//

#import "SYTradeInputView.h"
#import "SYKeyboardHeader.h"

@interface SYTradeInputView ()

@end

@implementation SYTradeInputView


- (instancetype)initWithFrame:(CGRect)frame {
    if (self = [super initWithFrame:frame]) {
        self.backgroundColor = [UIColor clearColor];
        [self setupSubviesw];
    }
    return self;
}

- (void)drawRect:(CGRect)rect {
    // 画图
    UIImage *bg = [UIImage imageNamed:@"pssword_bg"];
    [bg drawInRect:rect];
    
    // 画字
    NSString *title = self.titleStr;
    
    CGSize size = [title boundingRectWithSize:CGSizeMake(MAXFLOAT, MAXFLOAT) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:SYKeyboardScreenWidth * 0.053125]} context:nil].size;
    CGFloat titleW = size.width;
    CGFloat titleH = size.height;
    CGFloat titleX = (self.frame.size.width - titleW) * 0.5;
    CGFloat titleY = SYKeyboardScreenWidth * 0.03125;
    CGRect titleRect = CGRectMake(titleX, titleY, titleW, titleH);
    
    NSMutableDictionary *attr = [NSMutableDictionary dictionary];
    attr[NSFontAttributeName] = [UIFont systemFontOfSize:SYKeyboardScreenWidth * 0.053125];
    attr[NSForegroundColorAttributeName] = SYColor(102, 102, 102);
    
    [title drawInRect:titleRect withAttributes:attr];
    
    //  画框
    NSString *image = self.maximum == 6 ? @"password_in_6" : @"password_in_8";
    UIImage *field = [UIImage imageNamed:image];
    CGFloat x = SYKeyboardScreenWidth * 0.153125 * 0.5;
    CGFloat y = SYKeyboardScreenWidth * 0.40625 * 0.5;
    CGFloat w = SYKeyboardScreenWidth * 0.846875;
    CGFloat h = SYKeyboardScreenWidth * 0.121875;
    [field drawInRect:CGRectMake(x, y, w, h)];
    
    // 画点
    UIImage *pointImage = [UIImage imageNamed:@"yuan"];
    CGFloat pointW = SYKeyboardScreenWidth * 0.05;
    CGFloat pointH = pointW;
    CGFloat pointY = SYKeyboardScreenWidth * 0.24;
    CGFloat pointX;
    CGFloat margin = SYKeyboardScreenWidth * 0.0765625;
    CGFloat padding = SYKeyboardScreenWidth * 0.045578125;
    if (self.maximum == 8) {
        padding = SYKeyboardScreenWidth * 0.027929688;
    }
    for (int i = 0; i < self.nums.count; i++) {
        pointX = margin + padding + i * (pointW + 2 * padding);
        [pointImage drawInRect:CGRectMake(pointX, pointY, pointW, pointH)];
    }
    
    if (self.nums.count == self.maximum) {
        NSString *password = [self.nums componentsJoinedByString:@""];
        if (self.delegate && [self.delegate respondsToSelector:@selector(password:)]) {
            [self.delegate password:password];
        }
    }
}

- (void)setupSubviesw {
    NSString *title = @"请输入支付密码";
    CGSize size = [title boundingRectWithSize:CGSizeMake(MAXFLOAT, MAXFLOAT) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:SYKeyboardScreenWidth * 0.053125]} context:nil].size;
    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    button.frame = CGRectMake(10, SYKeyboardScreenWidth * 0.03125 + size.height/2 - 18, 36, 36);
    [self addSubview:button];
    [button setImage:[UIImage imageNamed:@"fanhui"] forState:UIControlStateNormal];
    [button addTarget:self action:@selector(handlePopAction:) forControlEvents:UIControlEventTouchUpInside];
}

- (void)handlePopAction:(UIButton *)button {
    if (self.delegate && [self.delegate respondsToSelector:@selector(popAction)]) {
        [self.delegate popAction];
    }
}

- (NSMutableArray *)nums {
    if (_nums == nil) {
        _nums = [NSMutableArray array];
    }
    return _nums;
}
@end
